// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
import About from "@/pages/About.vue";
import Home from "@/pages/Home.vue";
import News from "@/pages/News.vue";
import Message from "@/pages/Message.vue";
import Detail from "@/pages/Detail.vue";

const vueRouter = new VueRouter({
    mode: 'history',
    routes:[
        {
            name: 'guanyu',
            path: '/about',
            component: About,
            meta:{title: 'guanyu'}
        },
        {
            path: '/home',
            component: Home,
            meta:{title: 'home'},
            children:[
                {
                    path: 'news',   // 此处不要带 /
                    component: News,
                    meta:{isAuth: true, title: 'news'},
                    // beforeEnter: (to, from, next) => {
                    //     if (localStorage.getItem('school') === 'atguigu'){
                    //         next()
                    //     }else {
                    //         alert('学校名不对，无权限')
                    //     }
                    // }
                },
                {
                    path: 'message',
                    component: Message,
                    meta:{isAuth: true, title: 'message'},
                    children:[
                        {
                            meta:{title: 'detail'},
                            name: 'xiangqing',
                            path: 'detail/:id/:title',   // 此处不要带 /
                            component: Detail,
                            // props: {a:1, b:'hello'}
                            props: true
                        }
                    ]
                },

            ]
        },
    ]
})

// vueRouter.beforeEach((to,from,next)=>{
    // console.log('==>', to, from, next);
    // if (to.path === '/home/news' || to.path === '/home/message'){
    //     if (localStorage.getItem('school') === 'atguigu'){
    //         next()
    //     }else {
    //         alert('学校名不对，无权限')
    //     }
    // }else {
    //     next()
    // }

    // document.title = '哈哈'
    // if (to.meta.isAuth){
    //     if (localStorage.getItem('school') === 'atguigu'){
    //         next()
    //     }else {
    //         alert('学校名不对，无权限')
    //     }
    // }else {
    //     next()
    // }
// })

vueRouter.afterEach((to,from)=>{
    document.title = to.meta.title
})


export default vueRouter;
